<?php

error_reporting(E_ALL & ~E_NOTICE);
date_default_timezone_set('Asia/Ho_Chi_Minh');

session_start();

define('DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
include DIR . 'library/ChipVN/Loader.php';

\ChipVN\Loader::registerAutoLoad();


// fiter
$params = array('server', 'resize', 'watermark', 'logo');
foreach ($params as $param) {
    $name = $param . 'id';
    $data = intval($_REQUEST[$param]);
    if ($data < 0) {
        $data = 0;
    }
    ${$name} = $data;
}


##################### START CONFIG #######################

$sitename = 'chiplovebiz';
/**
 * Tạo và CHMOD folder này sang 777
 */
$tempdir = DIR . 'temp/';

// danh sách logo
$logolist = array(
    1 => 'logo1.png',
    2 => 'logo2.png',
    3 => 'logo3.png',
);
// Nếu logo yêu cầu ko có trong danh sách thì dùng logo1.png 
$default['logo'] = 'logo1.png';

// vị trí logo (right bottom, right center, right top, left top, .v.v.)
$logoPosition = 'rb';



// kích cỡ resize
$resizelist = array(
    0 => 0, // ko resize
    1 => 100,
    2 => 150,
    3 => 320,
    4 => 640,
    5 => 800,
    6 => 1024
);
$default['resize'] = 800;


##################### END CONFIG #######################



$watermark = $watermarkid > 0 ? TRUE : FALSE;

$logoPath = DIR . 'logo/' . (in_array($logoid, array_keys($logolist)) ? $logolist[$logoid] : $default['logo']);

$resizeWidth = in_array($resizeid, array_keys($resizelist)) ? $resizelist[$resizeid] : $default['resize'];

$url = 'http://demo.theme-junkie.com/wordplus/files/2013/07/300x250a.gif';
$isUpload = FALSE;
\ChipVN\Image::leech($url, $imagePath = $tempdir . $sitename . date('dmY') . '.jpg');

$service = 'Picasa';
$uploader = \ChipVN\Image_Uploader::factory($service);
$uploader->login('socheate@gmail.com', '0689989@sn');

if (!$imagePath) {
    die('Mising an image');
}
$url = $uploader->upload($imagePath);

if (file_exists($imagePath)) {
    unlink($imagePath);
}

if ($isUpload) {
    echo 'image=' . $url;
} else {
    echo $url;
}
